/* Copyright (C) 2008  Romolo Camplani, Marco Fortunato, Marco Marelli, Guido Rota, Fabio A. Schreiber et al.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.dei.perla.parser.grammar.context;

import org.dei.perla.parser.clauses.HLEveryClause;
import org.dei.perla.parser.clauses.HavingClause;
import org.dei.perla.parser.clauses.OnEmptySelectionClause;
import org.dei.perla.parser.clauses.WhereClause;
import org.dei.perla.parser.expressions.ConstantLogic;
import org.dei.perla.parser.expressions.LogicValue;
import org.dei.perla.parser.expressions.NodeConstant;
import org.dei.perla.parser.statements.HLSelectionDefinition;
import org.dei.perla.parser.statements.HLSelectionDefinitionList;

/**
 * Classe contenente i parametri degli statement di alto livello del contesto corrente durante il parsing
 */
public class HLContext {
	
	/**
	 * Oggetto HLEveryClause corrente
	 */
	public static HLEveryClause everyClause = null;
	
	/**
	 * Oggetto HLSelectionDefinitionList per la memorizzazione delle HLSelectionDefinition parserizzate
	 */
	public static HLSelectionDefinitionList selectionDefinitionList = null;
	
	/**
	 * Oggetto OnEmptySelectionClause corrente
	 */
	public static OnEmptySelectionClause emptyClause = null;

	/**
	 * Resetta il contesto
	 */
	public static void clear() {
		
		everyClause = null;
		emptyClause = null;
		selectionDefinitionList = null;
	}
	
	/**
	 * Aggiunge, a partire dalle informazioni presenti nell'HLSelectionContext, un oggetto
	 * di tipo HLSelectionDefinition
	 */
	public static void addHLSelectionDefinition() {
		
		HLSelectionDefinition selection = new HLSelectionDefinition();
		
		// Aggiungo la clausola Select
		//TODO: Aggiungere il controllo dei campi nella select (sono quelli corrispondenti al From?
		// manca qualche qualificatore?)
		selection.setSelect(HLSelectionContext.selectClause);
		
		// imposta il flag per UNION [ALL]
		selection.setUnionAll(HLSelectionContext.unionAll);
		
		// Aggiungo la clausola From
		selection.setFromClause(HLSelectionContext.fromClause);
		
		//Aggiungo la clausola Where
		if (HLSelectionContext.whereClause == null) {
			
			WhereClause whereClause = new WhereClause();
			whereClause.setCondition(new NodeConstant(new ConstantLogic(true)));
			selection.setWhere(whereClause);
			
		} else {
			
			selection.setWhere(HLSelectionContext.whereClause);
		}
		
		// Aggiungo le clausole GroupBy e Having
		
		// Inserisco la clausola HavingClause nell'eventuale HLGroupClause
		if (HLSelectionContext.groupByClause != null) {
			
			// Creiamo la clausola Having di default
			if(HLSelectionContext.havingClause == null)
			{
				HLSelectionContext.havingClause = new HavingClause();
				ConstantLogic trueCondition = new ConstantLogic(LogicValue.TRUE);
				NodeConstant condition = new NodeConstant(trueCondition);
				HLSelectionContext.havingClause.setCondition(condition);
			}
			
			HLSelectionContext.groupByClause.setHavingClause(HLSelectionContext.havingClause);
		}
		
		selection.setGroupByClause(HLSelectionContext.groupByClause);
		
		// Aggiunge la HLSecectionDefinition appena creata al contesto corrente
		if (selectionDefinitionList == null) {
			
			selectionDefinitionList = new HLSelectionDefinitionList();
		}
		selectionDefinitionList.add(selection);
		
		// L'oggetto e' stato aggiunto, cancello il contesto per un altro eventuale parsing
		HLSelectionContext.clear();
	}
}
